package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.service.LogisticsService;
import com.cn.goodsadmin.service.UserAddressService;
import com.cn.goodsadmin.service.UserRealService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class CardAndRealController {

    @Autowired
    UserRealService userRealService;

    @Autowired
    UserAddressService userAddressService;

    @Autowired
    LogisticsService logisticsService;

    //@LoginRequired
    @RequestMapping(value = "/getAllChangeReal", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getAllChangeReal(@RequestParam(value = "status") int status,
                                      @RequestParam(value = "pageindex") int pageindex,
                                      @RequestParam(value = "pagesize") int pagesize,
                                      @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<UserRealDestDTO> pageQueryResult = userRealService.findAllUserReal(status, pageindex, pagesize,appid);
            List<UserRealDestDTO> list=pageQueryResult.getQueryResult();
            RealGoodsPageInfo realGoodsPageInfo=new RealGoodsPageInfo();
            List<RealGoodsInfo>  realGoodsInfos=new ArrayList<RealGoodsInfo>();
            for(UserRealDestDTO destDTO:list){
                //生产实物类
                RealGoodsInfo realgoods=new RealGoodsInfo();
                Userreal userreal=destDTO.getUserreal();
                UserInfo userInfo=destDTO.getUserInfo();
                int addr=userreal.getcAddrid();
                Useraddress useraddress=userAddressService.findUserAddress(addr);
                realgoods.useraddress=useraddress;
                if(userreal.getcLogisticsid()!=null) {
                    Logistics logistics = logisticsService.findLogisticsByLid(userreal.getcLogisticsid());
                    realgoods.logistics=logistics;
                }
                realgoods.userInfo=userInfo;
                realgoods.userreal=userreal;
                realGoodsInfos.add(realgoods);
            }
            realGoodsPageInfo.realGoodsInfos=realGoodsInfos;
            realGoodsPageInfo.pagecount=pageQueryResult.getPageCount();
            realGoodsPageInfo.pageIndex=pageQueryResult.getPageIndex();
            realGoodsPageInfo.pagesize=pageQueryResult.getPageSize();
            realGoodsPageInfo.totalcount=pageQueryResult.getTotalCount();

            supplyMap.put("realGoodsPageInfo",realGoodsPageInfo);

        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/updateUserreal", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateUserreal(@RequestBody Userreal userreal) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            userRealService.update(userreal);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/sendChangeOrder", method = RequestMethod.GET,params = {"userrealid","appid","logisticsid","logisticsname"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap sendChangeOrder(int  userrealid,int appid,String logisticsid,String logisticsname){
        SupplyMap supplyMap=new SupplyMap();
        try{
            userRealService.sendChangeOrder(userrealid,appid,logisticsid,logisticsname);
        }catch (Exception e){
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
